package com.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.User;
import com.woniu.exception.BusinessException;
import com.woniu.mapper.UserMapper;
import com.woniu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname UserServiceImpl
 * @Description TODO
 * @Date 2025/8/5 11:12
 * @Created by pc
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    @Override
    public PageInfo<User> findAll(Integer currentPage,Integer pageSize) {
        // 分页
        PageHelper.startPage(currentPage,pageSize);
        List<User> all = userMapper.findAll();
        PageInfo<User> pageInfo = new PageInfo<>(all);
        return pageInfo;
    }
    // 1.程序运行中 本身会出现的异常  空指针异常 类型转换异常 sql异常
    // 2.自定义业务异常

    @Override
    public Map<String,Object> login(User user) throws Exception{
        // 1. 根据用户提交的用户名去查询 是否存在该用户
        User loginUser = userMapper.findByName(user.getName());
        Map<String,Object> map = new HashMap<>();
        if(loginUser == null){
            // map.put("code",500);
            // map.put("msg","用户不存在");
            throw new BusinessException("用户名不存在");
        }else if(!loginUser.getPassword().equals(user.getPassword())){  // 走到这里证明查询到用户了 比对密码
            // map.put("code",500);
            // map.put("msg","用户名存在 密码不正确");
            throw new BusinessException("用户名存在 密码不正确");
        }else{
            map.put("code","200");
            map.put("msg","登录成功");
            map.put("data",loginUser);
        }
        return map;
    }
}
